
/*
 * @Description:Map
 * 
 * @Author: rendc
 * 
 * @Date: 2025-08-14 09:21:50
 * 
 * @LastEditors: rendc
 * 
 * @LastEditTime: 2025-08-14 11:42:55
 */
// import java.security.KeyStore.Entry;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class TestMap {

  public static void main(String[] args) {
    // 创建一个HashMap集合
    Map<Integer, String> map = new HashMap<>();
    // 添加元素 给指定的key赋value值
    String v0 = map.put(0, "hello");
    String v1 = map.put(1, "hello");
    String v2 = map.put(0, "hello0"); // 会将hello覆盖为hello0
    System.out.println("v0:" + v0);
    System.out.println("v1:" + v1);
    System.out.println("v2:" + v2);
    System.out.println("map.size():" + map.size());
    System.out.println("map:" + map);
    // 是用map.put时 要先判断这里有没值
    int key = 3;
    System.out.println(map.containsKey(key));
    if (!map.containsKey(key)) {
      map.put(key, "newValue");
    }
    System.out.println("map.size():" + map.size());
    System.out.println("map:" + map);
    // 通过key查询value值
    System.out.println("0:" + map.get(0));
    System.out.println("1:" + map.get(1));
    System.out.println("2:" + map.get(2));
    System.out.println("3:" + map.get(3));
    System.out.println("4:" + map.get(4));
    System.out.println(map.remove(0));
    System.out.println(map.remove(4));
    System.out.println("map.size():" + map.size());
    System.out.println("map:" + map);
    // map.clear();
    // System.out.println("map.size():" + map.size());
    // System.out.println("map:" + map);
    // 遍历 方法1
    Set<Integer> set = map.keySet();
    System.out.println(set);
    for (Integer i : set) {
      // map.set
      System.out.println(map.get(i));
    }
    // 遍历 方法2
    Collection<String> coll = map.values();
    for (String s : coll) {
      System.out.println(s);
    }

    map.put(7, null);
    map.put(8, null);
    map.put(9, null);
    map.put(null, "1");
    map.put(null, "2");
    map.put(null, "3");

    // 遍历 方法3 entrySet()
    Set<Map.Entry<Integer, String>> entrys = map.entrySet();
    for (Entry<Integer, String> entry : entrys) {
      // 遍历的同时修改值
      // entry.setValue("hi");
      System.out.println(entry.getKey() + "-" + entry.getValue());
    }
  }

}